package org.top10.parser;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.cyberneko.html.parsers.DOMParser;
import org.top10.collector.Collector;
import org.top10.dealor.ConfReader;
import org.top10.dealor.FileDealor;
import org.top10.entity.CollectEntity;
import org.top10.entity.ConfBean;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class TudouTask extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_TUDOU;// 对应网站ID
	private static final String ENCoding = "gb2312";// 对应网站ID
	public TudouTask() {
		super(webSiteID,ENCoding);
	}

	public void loadParser() {
		PARSER.add(new TudouImakeParser());
		PARSER.add(new TudouHotPointParser());
		PARSER.add(new TudouFunMoiveParser());
		PARSER.add(new TundouBlackbeansMoiveParser());
	}

	public static void main(String args[]) throws Exception {
		//ParseSource source = Collector.createCache("http://imake.tudou.com/",ENCoding);
		new TundouBlackbeansMoiveParser().dealText(ENCoding);
	}
}

abstract class TudouParser extends TOPParserImpl {

	public TudouParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;
	public abstract Video build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..

//	protected Video buildVideo(Node node, int rank){
//		Video v = new Video();
//		v.setRank(rank);
//		List<Node> as = new ArrayList<Node>();
//		getAllTagsByParent(node, "a", as);
//		if(as.size() == 0)return  null;
//		String url=getAttrValueByTag(as.get(0),"href");
//		if (url.indexOf("programs/view/")!=-1) {
//			v.setPlayUrl(url);
//			v.setName(getAttrValueByTag(as.get(0), "title"));
//			//http://www.tudou.com/programs/view/
//			return v;
//		}
//		return null;
//	}
	protected Video buildVideo(Node node, int rank,String nameTag){
		Video v = new Video();
		v.setRank(rank);
		List<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		if(as.size() == 0)return  null;
		String url=getAttrValueByTag(as.get(0),"href");
		if (url.indexOf("programs/view/")!=-1||url.indexOf("www.tudou.com/playlist")!=-1) {
			v.setPlayUrl(url);
			v.setName(getAttrValueByTag(as.get(0), nameTag));
			//http://www.tudou.com/programs/view/
			return v;
		}
		return null;
	}
	protected List tudouParse(Document doc, String sign) throws Exception {
		List<Video> list = new ArrayList<Video>();
		Node body = doc.getElementsByTagName("BODY").item(0);
		Pattern p = Pattern.compile(sign, Pattern.MULTILINE);
		Node node = getNodeByText(body, "h2", sign);
		node=node.getParentNode().getNextSibling().getNextSibling();
	//	node=getNodeByPattern(node,"ol");
		List<Node> lis = new ArrayList<Node>();
		getAllTagsByParent(node, "li", lis);
		int rank=1;
		for (int i = 0; i < lis.size(); i++) {
			Video v = build(lis.get(i), rank);
			if(v!=null){
				System.out.println(v.toString());
				list.add(v);
				rank++;
			}
		}
		return list;
	}
}

class TudouTVParser extends TudouParser{
	public TudouTVParser(String c,  String u) {
		super(c,TOP10Global.TYPE_Television, u);
	}

	private final static String SIGN = "黑豆排行榜";

	public Video build(Node node, int rank) throws Exception {
		
		return buildVideo(node,rank,"itemname");
	}

	public List parse(ParseSource doc) throws Exception {
		return tudouParse(doc.getDocSource(),SIGN);
	}
}
class TudouImakeParser extends TudouParser{
	public TudouImakeParser() {
		super("播客排行",TOP10Global.TYPE_Television,"http://imake.tudou.com/");
	}

	private final static String SIGN = "播客视频排行";

	public Video build(Node node, int rank) throws Exception {
		
		return buildVideo(node,rank,"title");
	}

	public List parse(ParseSource doc) throws Exception {
		return tudouParse(doc.getDocSource(),SIGN);
	}
}

class TudouHotPointParser extends TudouParser{
	public TudouHotPointParser() {
		super("热点排行",TOP10Global.TYPE_Television,"http://news.tudou.com/");
	}

	private final static String SIGN = "热点排行榜";

	public Video build(Node node, int rank) throws Exception {
		
		return buildVideo(node,rank,"itemname");
	}

	public List parse(ParseSource doc) throws Exception {
		return tudouParse(doc.getDocSource(),SIGN);
	}
}
class TudouFunMoiveParser extends TudouParser{
	public TudouFunMoiveParser() {
		super("搞笑排行",TOP10Global.TYPE_Television, "http://fun.tudou.com/");
	}

	private final static String SIGN = "剧集排行榜";

	public Video build(Node node, int rank) throws Exception {
		
		return buildVideo(node,rank,"itemname");
	}

	public List parse(ParseSource doc) throws Exception {
		return tudouParse(doc.getDocSource(),SIGN);
	}
}
class TundouPotatoMoiveParser extends TudouParser{
	private final static String SIGN = "播放排行榜";
	public TundouPotatoMoiveParser(String c, String url) {
		super(c, TOP10Global.TYPE_Television, url);
	}
	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank,"");
	}
	public List parse(ParseSource source) throws Exception {
			List<Video> list = new ArrayList<Video>();
			Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
			Pattern p = Pattern.compile(SIGN, Pattern.MULTILINE);
			Node node = getNodeByPattern(body, "h2", p).getParentNode().getNextSibling().getNextSibling();
			List<Node> ol = new ArrayList<Node>();
			getAllTagsByParent(node,"ol",ol);
			List<Node> lis = new ArrayList<Node>();
			getAllTagsByParent(ol.get(1), "li", lis);
			for (int i = 0; i < lis.size(); i++) {
				Video v = build(lis.get(i), i + 1);
				if(v!=null){
					System.out.println(v.toString());
					list.add(v);
				}
			}
			return list;
	}
}

class TundouBlackbeansMoiveParser extends TudouParser {
	private final static String SIGN = "热门排行榜";

	public TundouBlackbeansMoiveParser() {
		super("黑豆排行", TOP10Global.TYPE_Television, "http://movie.tudou.com/hd/");
	}

	public List parse(ParseSource source) throws Exception {
		List<Video> list = new ArrayList<Video>();
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Pattern p = Pattern.compile(SIGN, Pattern.MULTILINE);
		Node node = getNodeByPattern(body, "h2", p).getParentNode().getNextSibling().getNextSibling();
		node=getNodeByPattern(node,"ol");
		List<Node> lis = new ArrayList<Node>();
		getAllTagsByParent(node, "li", lis);
		int rank=1;
		for (Node video: lis) {
			Video v = build(video, rank);
			if(v!=null){
				System.out.println(v.toString());
				list.add(v);
				rank++;
				if(rank>10)break;
			}
		}
		return list;
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank,"title");
	}
}
